<template>
  <div>
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item>
        <el-input
          v-model="formInline.title"
          placeholder="请输入专题标题"
          size="mini"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="formInline.subtitle"
          placeholder="请输入专题子标题"
          size="mini"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-select v-model="value" size="mini" placeholder="按时间排序">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option> </el-select
      ></el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          @click="onSubmit"
          size="mini"
          icon="el-icon-search"
          >查找</el-button
        >
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="mini" icon="el-icon-download"
          >导出</el-button
        >
      </el-form-item>
    </el-form>
    <el-form style="float: right">
      <el-form-item>
        <el-button
          type="primary"
          @click="addopen"
          size="mini"
          icon="el-icon-edit"
          >添加</el-button
        >
        <el-button
          type="danger"
          @click="batchDelete"
          size="mini"
          icon="el-icon-edit"
          >批量删除</el-button
        >
      </el-form-item>
    </el-form>
    <div class="tables">
      <template>
        <el-table
          border
          :data="tableData"
          style="width: 100%"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55"> </el-table-column>
          <el-table-column prop="title" label="专题标题" align="center">
          </el-table-column>
          <el-table-column prop="subtitle" label="专题子标题" align="center">
          </el-table-column>
          <el-table-column prop="picUrl" label="图片" align="center">
            <template slot-scope="scope">
              <img :src="scope.row.picUrl" width="50" height="50" alt="" />
            </template>
          </el-table-column>
          <el-table-column prop="content" label="专题详情" align="center">
            <template slot-scope="scope">
              <el-button type="primary" size="mini" @click="look(scope.row)">
                查看
              </el-button>
            </template>
          </el-table-column>
          <el-table-column prop="price" label="底价" align="center">
          </el-table-column>
          <el-table-column prop="readCount" label="阅读数量" align="center">
          </el-table-column>
          <el-table-column
            prop="address"
            label="操作"
            align="center"
            width="200"
          >
            <template slot-scope="scope">
              <el-button type="primary" size="mini" @click="edit(scope.row)">
                编辑
              </el-button>
              <el-button
                size="mini"
                type="danger"
                @click="dels(scope.$index, scope.row)"
              >
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
      <div class="page">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="get.page"
          :page-sizes="[10, 20, 30, 40, 50, 100]"
          :page-size="get.limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </div>
    <!-- ========================= -->
    <el-dialog
      title="专题详情"
      :data="tableData"
      :visible.sync="dialogVisible"
      width="50%"
    >
      <template slot-scope="scope">
        <el-image> {{ scope.row.content }}</el-image>
      </template>
    </el-dialog>
    <!-- ------------------------------ -->
    <el-dialog
      title="专题详情"
      :visible.sync="centerDialogVisible"
      width="55%"
      center
    >
      <div v-html="desc" style="text-align: center"></div>
    </el-dialog>
  </div>
</template>

<script>
import { artList, redact, artdel,dellll, add, search } from "@/api/promotion";
import moment from "moment";
export default {
  data() {
    return {
      desc:"",
      flag: true,
      centerDialogVisible: false,
      formLabelWidth: "120px",
      dialogVisible: false,
      form: {
        goodsId: "",
        discount: "",
        discountMember: "",
        expireTime: moment().format("YYYY-MM-DD HH:mm:ss"),
      },
      tableData: [],
      formInline: {},
      total: 0,
      get: {
        page: 1,
        limit: 20,
        title: "",
        subtitle: "",
        sort: "add_time",
        order: "desc",
      },
      options: [
        {
          value: "选项1",
          label: "按序号排序",
        },
        {
          value: "选项2",
          label: "按时间排序",
        },
        {
          value: "选项3",
          label: "按价格排序",
        },
      ],
      value: "",
      multipleSelection: [],
      rules: {
        goodsId: [{ required: true, message: "商品不能为空", trigger: "blur" }],
        discount: [
          { required: true, message: "团购折扣不能为空", trigger: "blur" },
        ],
        discountMember: [
          { required: true, message: "团购人数不能为空", trigger: "blur" },
        ],
        expireTime: [
          { required: true, message: "过期时间不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getdata();
  },
  methods: {
    getdata() {
      artList(this.get).then((res) => {
        console.log(res);
        if ((res.data.errmsg = "成功")) {
          this.tableData = res.data.list;
          this.total = res.data.total;
          console.log(this.tableData);
        }
      });
    },
    // 查询
    onSubmit() {
      this.get.page = 1;
      this.get.title = this.formInline.title;
      this.get.subtitle = this.formInline.subtitle;
      this.getdata();
    },

    // 编辑
    edit(row) {
      console.log(row);
      this.$router.push({
        name: "artadd",
        query: {
          tab: "edit",
          title: row.title,
          subtitle: row.subtitle,
          picUrl: row.picUrl,
          content: row.content,
          price: row.price,
          readCount: row.readCount,
          goods: row.goods,
        },
      });
    },
    // 查看
    look(res) {
      // console.log(res.detail);
      this.desc = res.content;
      this.centerDialogVisible = true;
    },
    // 删除
    dels(a, b) {
      artdel(b).then((res) => {
        if (res.errmsg == "成功") {
          this.getdata();
          this.$notify({
            title: "成功",
            message: "删除成功",
            type: "success",
          });
        }
      });
    },
    //批量删除
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    batchDelete() {
      if (this.multipleSelection.length === 0) {
        this.$message({
          message: "请选择要删除的项",
          type: "warning",
        });
        return;
      }
      const ids = this.multipleSelection.map((item) => item.id); // 假设每条数据有一个唯一的id字段

      this.$confirm("确定要删除选中的项吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        dellll(ids)
          .then((res) => {
            if (res.errmsg === "成功") {
              this.getdata();
              this.$notify({
                title: "成功",
                message: "批量删除成功",
                type: "success",
              });
            }
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消删除",
            });
          });
      });
    },
    // 添加打开
    addopen() {
      this.$router.push({
        name: "artadd",
        query: { tab: "add" },
      });
    },
    //分页
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.get.limit = val;
      this.getdata();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.get.page = val;
      this.getdata();
    },
    formatDate(row, column, cellValue) {
      return moment(cellValue).format("YYYY-MM-DD HH:mm:ss");
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep .el-table td,
::v-deep .el-table th {
  padding: 4px 0px;
}
.el-table {
  font-size: 12px;
}
.page {
  margin-top: 40px;
  display: flex;
  justify-content: left;
}
.el-form-item {
  margin-bottom: 0;
}
</style>
